package com.cennavi.minenavidemo.activity.map;

import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.activity.SettingActivity;
import com.cennavi.minenavidemo.presenter.MapPresenter;
import com.cennavi.minenavidemo.presenter.PoiPresenter;
import com.cennavi.minenavidemo.widget.MyMapView;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.mapdal.LatLng;
import com.minedata.minenavi.mapdal.PoiItem;
import com.minedata.minenavi.mapdal.Tools;
import com.minedata.minenavi.poiquery.DistrictItem;
import com.minedata.minenavi.poiquery.RegeocodeAddress;
import com.minedata.minenavi.poiquery.Tip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class PoiSearchResultLayerActivity extends AppCompatActivity implements MapPresenter.OnMapViewListener,
        PoiPresenter.OnPoiSearchListener {


    private MineMap mMineMap;
    private MapPresenter mMapPresenter;
    private PoiPresenter mPoiPresenter;

    private Unbinder mUnbinder;
    @BindView(R.id.mapView)
    MyMapView mMapView;
    @BindView(R.id.etKeyword)
    EditText etKeyword;
    @BindView(R.id.btnSearch)
    Button btnSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poisearchresultlayer);
        mUnbinder = ButterKnife.bind(this);
        init();
    }

    public void init() {
        mMapPresenter = MapPresenter.getInstance();
        mMapPresenter.init(PoiSearchResultLayerActivity.this, mMapView);
        mMapPresenter.setOnMapViewListener(this);
        mPoiPresenter = PoiPresenter.getInstance();
        mPoiPresenter.init(PoiSearchResultLayerActivity.this);
        mPoiPresenter.setOnPoiSearchListener(this);
        mPoiPresenter.initPoiSearch();
    }

    @OnClick(R.id.btnSearch)
    public void keyWordSearch() {
        String key = etKeyword.getText().toString();

        if (TextUtils.isEmpty(key)) {
            Toast.makeText(this, "请输入查询关键字", Toast.LENGTH_LONG).show();
            return;
        }

        mMapPresenter.deletePoiSearchResultLayer();
        mPoiPresenter.startKeywordSearch(key);
    }

    @Override
    protected void onPause() {
        if (mMapView != null) {
            mMapView.onPause();
        }

        super.onPause();
    }

    @Override
    protected void onResume() {
        if (mMapView != null) {
            mMapView.onResume();
        }

        super.onResume();
    }

    @Override
    protected void onDestroy() {
        if (mMapView != null) {
            mMapView.onDestroy();
        }

        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onMapReady() {
        mMineMap = mMapPresenter.getMineMap();
        Point point = Tools.latLngToPoint(new LatLng(39.984059, 116.307771));
        mMineMap.setPointToCenter(point.x, point.y);
        mMineMap.setZoomLevel(11);
        mMapPresenter.initPoiSearchResultLayer();
    }

    @Override
    public void onswitchLocation(int imageLevel) {

    }

    @Override
    public void onSearchComplete(ArrayList<PoiItem> poiItems, boolean isSuccess) {
        if (isSuccess) {
            Point point = Tools.latLonPointToPoint(poiItems.get(0).location);
            mMineMap.setPointToCenter(point.x, point.y);
            mMapPresenter.showPoiSearchResultLayer(poiItems);
        } else {
            Toast.makeText(PoiSearchResultLayerActivity.this, "无结果。", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onIdSearch(PoiItem poiItem, boolean isSuccess) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeAddress regeocodeAddress) {

    }

    @Override
    public void onDistrictSearch(DistrictItem districtItem) {

    }

    @Override
    public void onGetInputtips(List<Tip> lists) {

    }
}
